package com.goldenautumn.prefabricate.widget.timeline

import android.content.Context
import android.graphics.Canvas
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.goldenautumn.prefabricate.R

class TimelineView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
    private val linePaint = Paint()
    private val dotPaint = Paint()
    private var timelinePosition = 0

    init {
        // 设置虚线的颜色和样式
        linePaint.color = ContextCompat.getColor(context, R.color.color_D7D7D7)
        linePaint.strokeWidth = context.resources.getDimension(qncx.external.common.R.dimen.dimen_2)
        linePaint.style = Paint.Style.STROKE
        linePaint.isAntiAlias = true
        linePaint.pathEffect = DashPathEffect(floatArrayOf(10f, 10f), 0f)

        // 设置小圆点的颜色和样式
        dotPaint.color = ContextCompat.getColor(context, qncx.external.common.R.color.transparent)
        dotPaint.style = Paint.Style.FILL
        dotPaint.isAntiAlias = true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 获取 View 的宽度和高度
        val width = width.toFloat()
        val height = height.toFloat()

        // 计算虚线的起始点和终止点的坐标
        val startX = width / 2
        val startY = 0f
        val endX = startX
        val endY = height

        // 绘制虚线
        canvas.drawLine(startX, startY, endX, endY, linePaint)

        // 计算小圆点的中心坐标
        val dotX = startX
        val dotY = if (timelinePosition == 0) startY else endY

        // 绘制小圆点
        canvas.drawCircle(dotX, dotY, DOT_RADIUS, dotPaint)
    }

    fun setTimelinePosition(position: Int) {
        timelinePosition = position
        invalidate()
    }

    companion object {
        private const val DOT_RADIUS = 10f
    }
}